package com.doodle.common.drawable;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.doodle.common.data.AsyncResource;
import com.doodle.common.data.DataAdapter.DataLoadedListener;
import com.doodle.common.utils.BitmapDrawableJob;
import com.doodle.common.utils.BitmapPool;
import com.doodle.common.utils.BitmapUtils;
import com.doodle.common.utils.ThreadPool.Job;

public class UriAsyncDrawable extends MeasuredAsyncDrawable {
	
	protected Context mContext;
	protected String mUri;
	protected String mDownloadFilePath;
	protected int mRoundCornerRadius = -1;
	
	public UriAsyncDrawable(Context context, String uri, int measuredWidth, int measuredHeight, int type,
							AsyncResource.JobExecutor<Drawable> jobExecutor, Drawable placeHolder, int id, DataLoadedListener listener,
							String downloadFilePath, int roundCornerRadius) {
		this(context, uri, measuredWidth, measuredHeight, BitmapUtils.FIT_LENGTH_LARGGER, type,
				jobExecutor, placeHolder, id, listener, downloadFilePath, roundCornerRadius);
	}
	
	public UriAsyncDrawable(Context context, String uri, int measuredWidth, int measuredHeight, int fitMode, int type,
							AsyncResource.JobExecutor<Drawable> jobExecutor, Drawable placeHolder, int id, DataLoadedListener listener,
							String downloadFilePath, int roundCornerRadius) {
		super(measuredWidth, measuredHeight, fitMode, type, jobExecutor, placeHolder, id, listener);
		mContext = context;
		mUri = uri;
		mDownloadFilePath = downloadFilePath;
		mRoundCornerRadius = roundCornerRadius;
	}
	
	public String getUri() {
		return mUri;
	}

	@Override
	public Job<Drawable> requestDrawable() {
		return new BitmapDrawableJob(mContext, mUri, mMeasuredWidth, mMeasuredHeight, mFitMode, mType, mDownloadFilePath, mRoundCornerRadius);
	}

	@Override
	public void recycleDrawable(Drawable drawable) {
		if (mType != DRAWABLE_TYPE_ICON) {
			BitmapPool.getPool(mType).recycle(((BitmapDrawable)drawable).getBitmap());
		}
	}

}
